<template>
    <div class="unit-bets">
        <div class="u-header">
            <div class="title">{{betss.Caption}}</div>
            <div class="input">
                金币
                <input type="number" :value="gold" @input="setGold">
            </div>
            <div class="hidden" @click="hiddenBody">隐藏</div>
        </div>
        <div class="u-body" :class="{hidden: hidden}" v-if="betss.type !== 'and'">
            <div
                    class="item"
                    :class="{chiose: getkey(k)}"
                    v-for="(item,k) of betss.OgMtrans"
                    @click="setField(item.Name, k)">
                <span
                        class="number"
                        :class="{
                            'num-1': item.Name === '1',
                            'num-2': item.Name === '2',
                            'num-3': item.Name === '3',
                            'num-4': item.Name === '4',
                            'num-5': item.Name === '5',
                            'num-6': item.Name === '6',
                            'num-7': item.Name === '7',
                            'num-8': item.Name === '8',
                            'num-9': item.Name === '9',
                            'num-10': item.Name === '10',
                }">{{item.Name}}</span>
                <span ref="odds" class="oddsr">{{item.odds}}</span>
            </div>
            <div class="item null" v-for="item of nullValue"></div>
        </div>
        <div class="u-body" :class="{hidden: hidden}" v-else>
            <div
                    class="item"
                    :class="{chiose: field === item.Name}"
                    @click="setField(item.Name, k)"
                    v-for="(item,k) in betss.OgMtrans">
                <span>{{item.Name}}</span>
                <span ref="odds" class="oddsr">{{item.odds}}</span>
            </div>
            <div class="item null" v-for="item of nullValue"></div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "unit",
        data () {
            return {
                field: [],
                hidden: false,
                nullValue: this.betss.OgMtrans.length % 3,
            }
        },
        props: ['defaultOdds', 'success', 'betss', 'gold'],
        methods: {
            getkey(key) {
                return this.field.some(val => {
                    return val === key
                })
            },
            setField (v, k) {
                let falg = this.field.some((val, key) => {
                    if (val === k) {
                        this.field.splice(key, 1)
                    }
                    return val === k
                })

                if (falg === false) {
                    this.field.push(k)
                }

                console.log(this.field)


                /*this.$set(this, 'field', v)
                this.$emit('fieldEvent', {
                    field: this.field,
                    type: this.betss.type,
                    odds: this.betss.OgMtrans[k].odds,
                    Groupno: this.betss.OgMtrans[k].Groupno,
                    Objectid: this.betss.OgMtrans[k].Objectid
                })*/
            },
            hiddenBody () {
                this.$set(this, 'hidden', ! this.hidden)
            },
            setGold(v) {
                // this.$set(this, 'gold', Math.abs(parseInt(v.target.value)))
                this.$emit('input', Math.abs(parseInt(v.target.value)))
            }
        },
        watch: {
            gold (val) {
                return this.$emit('update:gold', parseInt(val))
            },
            success () {
                //this.$set(this, 'field', '')
                //this.$set(this, 'gold', 0)
            }
        },
        filters: {
            capitalize: function (value, item, defaultOdds) {
                let odds = defaultOdds
                value.forEach(res => {
                    if (res.field == item) {
                        odds = res.odds
                    }
                })
                return odds
            }
        }
    }
</script>

<style scoped lang="less">
@import "../../assets/css/bases";
    .unit-bets {
        .u-header {
            height: 35px;
            background: @gray;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            .title {
                width: 120px;
                text-indent: 10px;
                font-size: 1.1rem;
            }
            .input {
                display: flex;
                justify-content: flex-start;
                align-items: center;
                input {
                    border: none;
                    border-radius: 5px;
                    background: #fff799;
                    width: 60px;
                    height: 20px;
                    outline: none;
                    text-align: center;
                    margin-left: 5px;
                }
            }
            .bets, .hidden {
                margin-left: 10px;
                text-decoration:underline
            }
        }
        .u-body {
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            .item {
                margin-top: 10px;
                border: @gray solid 1px;
                border-radius: 5px;
                width: 28%;
                padding: 5px;
                display: flex;
                justify-content: space-between;
                align-items: center;
                color: @error;
                .number {
                    width: 20px;
                    height: 20px;
                    line-height: 20px;
                    text-align: center;
                    font-size: @default;
                }
                span {
                    color: white;
                }
                .oddsr {
                    color: @error;
                }
            }
            .item.btn {
                background: @wran;
                color: white;
                border: @wran solid 1px;
                justify-content: center;
            }
            .item.null {
                border: none;
            }
            .item.chiose {
                border: #ccc solid 1px;
                background: #ccc;
            }
        }
        .u-body.hidden {
            display: none;
        }
    }
</style>